using System;
using System.Collections.Generic;
using TaskKernel.Infrastructure;

namespace TaskKernel.Domain.Services.Mail
{
    public class BodyTemplateContentAlexandrova : IBodyTemplate
    {
        private readonly string _bodyComplete;

        public BodyTemplateContentAlexandrova(Task task)
        {
            var body = new List<string> { "<table>" };
            foreach (var entry in task.HistoryEntries)
            {
                switch (entry.TaskHistoryEntryType)
                {
                    case HistoryEntryType.Comment:
                        if (!String.IsNullOrEmpty(entry.Comment))
                            GetValue(body, entry.DateCreate.ToString(), entry.Comment, entry.Owner.FullName);
                        break;
                    case HistoryEntryType.File:
                        if (entry.File != null)
                            GetValue(body, entry.File.DateCreate.ToString(), entry.File.FileName, entry.File.Owner.FullName);
                        break;
                }
            }
            body.Add("</table>");
            _bodyComplete = String.Join("", body.ToArray());
        }

        #region IBodyTemplate Members

        public string GetBody()
        {
            return _bodyComplete;
        }

        #endregion

        private void GetValue(List<string> body, string date, string text, string author)
        {
            var bodyPartTemplate = @"<tr><td valign=""top"">##DATECREATE##</td><td>##COMMENT##</td><td>##AUTHOR##</td></tr>";
            bodyPartTemplate = bodyPartTemplate.Replace("##DATECREATE##", date);
            bodyPartTemplate = bodyPartTemplate.Replace("##COMMENT##", text);
            bodyPartTemplate = bodyPartTemplate.Replace("##AUTHOR##", author);
            body.Add(bodyPartTemplate);
        }
    }
}